﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using System.Data;
using iLangl.Security;
using iLangl.BusinessModel.Managers;

namespace iLangl.BusinessModel.DAL.XMLStorage
{
	public class PluginDAL : BaseStorageDAL, IPluginDAL
	{
		/// <summary>
		/// Create Plugin License
		/// </summary>
		/// <param name="plugRegistration">Plugin Registration Request</param>
		/// <returns>License Key</returns>
		public String CreatePluginLicense(PluginRegistration plugRegistration, String userAccountId, IDbTransaction transaction)
		{
			//check if plugin exists
			PluginData plugin = ReadPlugin(plugRegistration.PluginId);

			if (plugin == null)
			{
				throw new ApplicationException(String.Format("Plugin with Id '{0}' is not found!", plugin.ID));
			}

			//Try to find if license exists
			foreach (dtoXmlStorage.PluginLicenseRow plr in Storage.PluginLicense)
			{
				if(
					(plr.PluginId == plugRegistration.PluginId)
					&&(plr.CompanyId == plugRegistration.Company.Id)
					&&(plr.UserAccountId == userAccountId))
				{
					return plr.LicenseKey;
				}
			}

			dtoXmlStorage.PluginLicenseRow license = Storage.PluginLicense.AddPluginLicenseRow(
				Guid.NewGuid().ToString(),
				Storage.Company.FindById(plugRegistration.Company.Id),
				Storage.Plugin.FindById(plugRegistration.PluginId),
				userAccountId,
				plugRegistration.PushURIAddress);


			if (transaction == null)
			{
				AcceptChanges();
			}

			return license.LicenseKey;
		}

		/// <summary>
		/// Register Plugin
		/// </summary>
		/// <param name="plugin">Plugin Information</param>
		public void RegisterPlugin(PluginData plugin, IDbTransaction transaction)
		{
			//Check if plugin Exists
			dtoXmlStorage.PluginRow pluginRow =  Storage.Plugin.FindById(plugin.ID);

			bool isNew = (pluginRow == null);
			if (isNew)
			{
				pluginRow = Storage.Plugin.NewPluginRow();
			}

			pluginRow.Id = plugin.ID;
			pluginRow.Name = plugin.Name;
			pluginRow.Version = plugin.Version;
			pluginRow.Platform = plugin.Platform;
			pluginRow.IsTranslationProvider = plugin.IsTranslationProvider;
			pluginRow.IsAsyncTransportSupported = plugin.IsAsyncTransportSupported;
			pluginRow.IsPushingEnabled = plugin.IsPushingEnabled;

			if(isNew)
				Storage.Plugin.AddPluginRow(pluginRow);

			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		/// <summary>
		/// Read Information About Plugin
		/// </summary>
		/// <param name="ID">Plugin Id</param>
		/// <returns>Plugin Info</returns>
		public PluginData ReadPlugin(String ID)
		{
			//Check if plugin Exists
			dtoXmlStorage.PluginRow pluginRow = Storage.Plugin.FindById(ID);

			if (pluginRow == null)
			{
				return null;
			}

			PluginData result = new PluginData();

			result.ID = pluginRow.Id;
			result.Name = pluginRow.Name;
			result.Version =  pluginRow.Version;
			result.Platform =  pluginRow.Platform;

			if(!pluginRow.IsIsTranslationProviderNull())
				result.IsTranslationProvider = pluginRow.IsTranslationProvider;

			if (!pluginRow.IsIsPushingEnabledNull())
				result.IsPushingEnabled = pluginRow.IsPushingEnabled;

			if (!pluginRow.IsIsPushingEnabledNull())
				result.IsPushingEnabled = pluginRow.IsPushingEnabled;

			if (!pluginRow.IsIsAsyncTransportSupportedNull())
				result.IsAsyncTransportSupported = pluginRow.IsAsyncTransportSupported;

			return result;
		}



		public PluginLicense ReadLicense(string ID)
		{
			dtoXmlStorage.PluginLicenseRow pluginlicenseRow = Storage.PluginLicense.FindByLicenseKey(ID);
			if (pluginlicenseRow == null)
				return null;

			PluginLicense result = new PluginLicense();
			result.ID = ID;
			result.PushURL = pluginlicenseRow.PushURL;
			result.Plugin = ReadPlugin(pluginlicenseRow.PluginId);
			result.UserAccountId = pluginlicenseRow.UserAccountId;

			String contactId = AuthenticationManager.Instance.ReadContactIdByUserAccountId(result.UserAccountId);
			result.Contact = DALFacade.Instance.ContactDAL.ReadContact(contactId);
			
			if(result.Contact.Company == null)
				result.Contact.Company = DALFacade.Instance.CompanyDAL.ReadCompany(pluginlicenseRow.CompanyId);
			return result;
		}



		#region IPluginDAL Members


		public PluginLicense ReadLicenseByUserAcount(string userAccountId)
		{
			foreach (dtoXmlStorage.PluginLicenseRow plr in Storage.PluginLicense)
			{
				if (plr.UserAccountId == userAccountId)
				{
					return ReadLicense(plr.LicenseKey);
				}
			}

			return null;
		}

		public PluginLicense ReadLicenseByCompanyId(string companyID)
		{
			foreach (dtoXmlStorage.PluginLicenseRow plr in Storage.PluginLicense)
			{
				if (plr.CompanyId == companyID)
				{
					return ReadLicense(plr.LicenseKey);
				}
			}

			return null;
		}

		#endregion
	}
}
